<template>
  <div class="container">
    <div style="width: 400px; padding: 30px; background-color: white; border-radius: 5px;">
      <div class="title" style="text-align: center; font-size: 32px; margin-bottom: 40px; color: #333; font-family: 'Roboto', sans-serif; text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);">
        <span class="gradient-text">欢迎登录EduCloud平台</span>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password  v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="管理员" value="ADMIN"></el-option>
            <el-option label="教师" value="TEACHER"></el-option>
            <el-option label="用户" value="USER"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%; background-color: #006eff; border-color: #006eff; color: white" @click="login">登 录</el-button>
        </el-form-item>
        <div style="display: flex; align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1; text-align: right">
            还没有账号？请 <a href="/register">注册</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: { role: '' },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  created() {

  },
  methods: {
    login() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/login', this.form).then(res => {
            if (res.code === '200') {
              localStorage.setItem("xm-user", JSON.stringify(res.data))  // 存储用户数据
              if (res.data.role === 'ADMIN') {
                location.href = '/manager/home'
              }
              if (res.data.role === 'TEACHER') {
                location.href = '/teacher/home'
              } else if (res.data.role === 'USER') {
                location.href = '/front/home'
              }
              this.$message.success('登录成功')
            } else {
              this.$message.error(res.msg)
            }
          }).catch(err => {
            this.$message.error('登录失败，请稍后重试');
            console.error(err);
          });
        }
      });
    }
  }}

</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/imgs/bg.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}
a {
  color: #2a60c9;
}
.login-header {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.title {
  margin-top: 20px;
}

.gradient-text {
  background: linear-gradient(90deg, #282828 0%, #58a8ce 100%);
  -webkit-background-clip: text;
  color: transparent;
  font-weight: bold;
  display: inline-block;
  margin: 0;
  animation: shine 5s linear infinite;
}

@keyframes shine {
  0% {
    background-position: -50%;
  }
  100% {
    background-position: 150%;
  }
}
</style>